import { imgUrl } from '../config.js'
import {apiPostLogin} from "@/api/login.js"
const METHOD = {
	GET: 'GET',
	POST: 'POST',
	PUT: 'PUT',
	DELETE: 'DELETE'
}

class Request {

	_header = {
		token:""
	}

	imgUrl = null

	interceptors = []

	constructor() {

	}

	intercept(res, resolve, reject) {
		var data = res
		this.interceptors.forEach(f => {
			var temp = f(data)
			if (temp.error) {
				reject(temp.error)
				return
			} else {
				data = temp.data
			}
		})
		resolve(data)
	}

	request({
		url,
		method,
		header = {},
		data = {}
	}) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: (this.imgUrl || '') + url,
				method: method || METHOD.GET,
				data: {
					...data
				},
				header: {
					...header,
					...this._header
				},
				success: res => this.intercept(res, resolve, reject),
				fail: res => {
					reject({
						errorCode: res.statusCode || -1,
						errorMessage: '无网络或配置异常'
					})
				}
			})
		})
	}

	get(url, data, header) {
		return this.request({
			url,
			method: METHOD.GET,
			header,
			data
		})
	}
	post(url, data, header) {
		return this.request({
			url,
			method: METHOD.POST,
			header,
			data
		})
	}
	put(url, data, header) {
		return this.request({
			url,
			method: METHOD.PUT,
			header,
			data
		})
	}
	delete(url, data, header) {
		return this.request({
			url,
			method: METHOD.DELETE,
			header,
			data
		})
	}
	header(header) {
		this._header = header
		return this
	}
	token(token) {
		this._header.Authorization = token
		return this
	}
	baseUrl(imgUrl) {
		this.imgUrl = imgUrl
		return this
	}
	interceptor(f) {
		if (typeof f === 'function') {
			this.interceptors.push(f)
		}
		return this
	}
}

const statusMap = {
	'100': '继续',
	'101': '转换协议',
	'200': '正常',
	'201': '已创建',
	'202': '接受',
	'203': '非官方信息',
	'204': '无内容',
	'205': '重置内容',
	'206': '局部内容',
	'300': '多重选择',
	'301': '文件已移动',
	'302': '找到',
	'303': '参见其他信息',
	'304': '未修正',
	'305': '使用代理',
	'307': '临时重定向',
	'400': '错误请求',
	'401': '未授权',
	'403': '禁止访问',
	'404': '未找到接口',
	'405': '方法未允许',
	'406': '无法访问',
	'407': '代理服务器认证要求',
	'408': '请求超时',
	'409': '冲突',
	'410': '已经不存在',
	'411': '需要数据长度',
	'412': '先决条件错误',
	'413': '请求实体过大',
	'414': '请求URI过长',
	'415': '不支持的媒体格式',
	'416': '请求范围无法满足',
	'417': '期望失败',
	'500': '服务器错误',
	'501': '未实现',
	'502': '错误的网关',
	'503': '服务无法获得',
	'504': '网关超时',
	'505': '不支持的 HTTP 版本'
}

class Http {
	constructor() {
		this.req = new Request()
		//初始化网络配置
		this.req.baseUrl(imgUrl)
			.header({
				'Accept': 'application/json,text/plain',
				// "device":'h5',
				"Authorization":uni.getStorageSync('token') || ''
			})
			.interceptor(res => {
				let result = {};
				console.log(res,87878)
				
				if (res && res.header && (res.header.Authorization || res.header.authorization)) {
					uni.setStorageSync('token', res.header.Authorization || res.header.authorization);
					this.req.token(res.header.Authorization || res.header.authorization)
				}
				if (res && res.statusCode >= 200 && res.statusCode < 300) {
					result.data = res.data;
				} else {
					uni.showToast({
						title: statusMap[res.statusCode + ''] || '',
						icon: 'none',
						duration: 1000
					})
					result.error = {
						errorCode: res.statusCode,
						errorMessage: statusMap[res.statusCode + ''] || ''
					}
				}
				return result;
			})
			
	}
	
	request(url, data){
		return this.req.get(url, data)
	}
	
	get(url, data) {
		return this.req.get(url, data, {
			'content-type': 'application/json'
		})
	}
	post(url, data) {
		return this.req.post(url, data, {
			'content-type': 'application/json'
		})
	}
	put(url, data) {
		return this.req.put(url, data, {
			'content-type': 'application/json'
		})
	}
	delete(url, data) {
		return this.req.delete(url, data, {
			'content-type': 'application/json'
		})
	}

}

export default new Http
